define(["jquery","bbcare","comm/datatable/datatable","as/app/dst.v2/dstList.servers","as/app/dst.v2/dstList.ui"],function($,$bb,$datatable,$servers,$ui){
	var dstList = {};


	/**
	 * 搜索成功后的回调函数
	 */
	var ajaxSearchCallBack = function(data){
		$ui.result({
            "id":"dstlistSearchResult",
            "list":data.rows,
        });
        $("#dstlistSearchResult li").unbind("click");
        $("#dstlistSearchResult li").click(function(){
        	$(".dstListModal").modal("hide");
        	var patientId = $(this).attr("data-patientId");
        	$bb.openPage("dstDetail", { patientId: patientId });
        });
	}

	/**
	 * 根据手机号码或姓名进行搜索
	 */
	var onSearch = function(){
		var text = $.trim( $("#searchText").val() );
		if( text ){
			var reg = /^1[3456789]\d{9}$/;
			var params = {"flag":"2"};
			if( !reg.test(text) ){
				params.patientName = text;
			}else{
				params.phoneNum = text;
			}
			$servers.ajaxSearch(params,ajaxSearchCallBack);
		}else{
			$("#dstlistSearchResult").html('');
		}
	}

	/**
	 * 新增
	 */
	var onAddClick = function(){
		var className = "dstListModal";
        $bb.modal({
            "className": className,
            "tit": "DST智能发育筛查",
        },function(){
            $("." + className).modal("show");
            $ui.search({
                "dom":$("." + className + " .modal-body")
            });
            $("#searchText").unbind("input propertychange");
            $("#searchText").bind("input propertychange",onSearch);
            $("#searchBtn").unbind("click");
            $("#searchBtn").bind("click",onSearch);
        });
	}

	dstList.init = function(){
		// datatable
        var id = $(".layout-bottom table").attr("id");
		$datatable.init({
            "id": id,
            "options": $servers.getOptions()
        });
        // 新增
        $("#addBtn").unbind("click");
		$("#addBtn").click(onAddClick);
		
		// 删除上次记录的患者id
		sessionStorage.getItem('dst_pateint_info_id') &&
		sessionStorage.removeItem('dst_pateint_info_id')
	}

	return dstList;
});